//
//  SensitiveSettingView.swift
//  SmartPenSwift
//
//  Created by tql on 2021/3/4.
//

import UIKit

class SensitiveSettingView: UIView {

    private var insideIndex = 0
    var currentIndex : Int {
        get{

            return insideIndex
        }
        set{
            
            insideIndex = newValue
            
            for item : UIView in self.bgView.subviews  {
                
                if item.isKind(of: UIButton.classForCoder()) {
                    if item.tag >= 100 {
                        let btntag : UIButton = item as! UIButton
                        if btntag.tag == insideIndex + 100  {
                            btntag.isSelected = true
                            btntag.backgroundColor = UIColor(hex: "EF7A4E")
                        }else{
                            btntag.isSelected = false
                            btntag.backgroundColor = UIColor(hex: "FEF1ED")
                        }
                    }
                }
            }
            
            
        }
    }
    typealias CallBackBlock = (_ changeIndex : Int) -> ()
    var changeIndexBLock : CallBackBlock?
    func show() -> Void {
        let window : UIWindow = (UIApplication.shared.delegate?.window ?? UIApplication.shared.connectedScenes.first as! UIWindow)!
        window.addSubview(self)
        self.frame = window.frame
    }
    lazy var bgView : UIView = {
        let view : UIView = UIView.init()
        view.backgroundColor = .white
        view.layer.cornerRadius = 20
        return view
    }()
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        createUI()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func createUI() -> Void {
        debugPrint(#function)

        self.backgroundColor = .clear
        
        let alphaView : UIView = UIView.init()
        self.addSubview(alphaView)
        alphaView.backgroundColor = UIColor.black.withAlphaComponent(0.3)
        alphaView.snp.makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        alphaView.isUserInteractionEnabled = true
        alphaView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(touchAlpView(_ :))))
        
        self.addSubview(self.bgView)
        self.bgView.snp.makeConstraints { (make) in
            make.bottom.equalTo(self).offset(20)
            make.height.equalTo(231+20)
            make.left.right.equalTo(self)
        }
        let lael1 : UILabel = {
            let lb = UILabel.init()
            lb.text = "设置灵敏度"
            lb.font = UIFont.systemFont(ofSize: 16)
            lb.textColor = UIColor.black
            return lb
        }()
        self.bgView.addSubview(lael1)
        lael1.snp.makeConstraints { (make) in
            make.top.equalTo(self.bgView).offset(30)
            make.left.equalTo(self.bgView).offset(30)
        }
        
        
        let label2 : UILabel = {
            let lb = UILabel.init()
            lb.text = "(灵敏度)"
            lb.font = UIFont.systemFont(ofSize: 16)
            lb.textColor = UIColor.black
            return lb
        }()
        self.bgView.addSubview(label2)
        label2.snp.makeConstraints { (make) in
            make.left.equalTo(lael1)
            make.top.equalTo(lael1.snp_bottomMargin).offset(20)
        }
        
        let gap : CGFloat = 20
        let btnSizeWidth : CGFloat = 50
        var leftGap : CGFloat = (kScreenWidth - btnSizeWidth * 5 - 4 * gap) / 2.0
        
        for i in 0..<5 {
            let btn : UIButton = UIButton.init()
            btn.setTitle("\(i)", for: .normal)
            btn.setTitleColor(.white, for: .selected)
            btn.setTitleColor(.black, for: .normal)
            self.bgView.addSubview(btn)
            btn.snp.makeConstraints { (make) in
                make.width.height.equalTo(btnSizeWidth)
                make.left.equalTo(self.bgView).offset(leftGap)
                make.top.equalTo(label2.snp_bottomMargin).offset(10)
            }
            btn.tag = i + 100
            btn.addTarget(self, action: #selector(btnclick(_ :)), for: .touchUpInside)
            btn.layer.cornerRadius = 13
            leftGap += btnSizeWidth + gap
        }
        
        let confirmBtn : UIButton = UIButton.init()
        confirmBtn.setTitle("确定", for: .normal)
        confirmBtn.setTitleColor(.white, for: .normal)
        self.bgView.addSubview(confirmBtn)
        confirmBtn.snp.makeConstraints { (make) in
            make.width.height.equalTo(btnSizeWidth)
            make.left.right.equalTo(self.bgView).inset(25)
            make.top.equalTo(label2.snp_bottomMargin).offset(10+60)
        }
        confirmBtn.addTarget(self, action: #selector(confirmBtncick(_ : )), for: .touchUpInside)
        confirmBtn.layer.cornerRadius = 13
        confirmBtn.backgroundColor = UIColor(hex: "EF7A4E")
        
        
        let cancelBtn : UIButton = UIButton.init()
        cancelBtn.setTitle("取消", for: .normal)
        cancelBtn.setTitleColor(UIColor(hex: "8492A6"), for: .normal)
        self.bgView.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { (make) in
            make.right.equalTo(self.bgView).offset(-25)
            make.top.equalTo(self.bgView).offset(20)
        }
        cancelBtn.addTarget(self, action: #selector(cancelBtncick(_ : )), for: .touchUpInside)
//        cancelBtn.layer.cornerRadius = 13
//        cancelBtn.backgroundColor = UIColor(hex: "EF7A4E")
        
        
        
    }
    @objc func cancelBtncick(_ btn : UIButton){
        self.removeFromSuperview()
    }
    @objc func confirmBtncick(_ btn : UIButton){
        var selectedIndex : Int = 0
        
        for item : UIView in self.bgView.subviews  {
            
            if item.isKind(of: UIButton.classForCoder()) {
                if item.tag >= 100 {
                    let btntag : UIButton = item as! UIButton
                    if btntag.isSelected == true  {
                        
                        selectedIndex = btntag.tag - 100
                        
                        break
                    }
                }
            }
        }
        
        if self.changeIndexBLock != nil {
            self.changeIndexBLock!(selectedIndex)
        }
        self.removeFromSuperview()
        
    }
    @objc func btnclick(_ btn : UIButton){
        if btn.isSelected == true {
            return
        }
        self.unseleteOther(btn)
        btn.isSelected = true
        btn.backgroundColor = UIColor(hex: "EF7A4E")
        
    }
    func unseleteOther(_ btn : UIButton) -> Void {
        for item : UIView in self.bgView.subviews  {
            
            if item.isKind(of: UIButton.classForCoder()) {
                if item.tag >= 100 {
                    let btntag : UIButton = item as! UIButton
                    if btn.isEqual(btntag) == false  {
                        btntag.isSelected = false
                        btntag.backgroundColor = UIColor(hex: "FDF1ED")
                    }
                }
            }
        }
    }
    
    @objc func touchAlpView(_ tap : UITapGestureRecognizer) -> Void {
        debugPrint(#function)
        self.removeFromSuperview()
    }
}
